package org.example.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

public class Demo01JDBC {
    public static void main(String[] args) throws Exception {
        //1.加载数据库驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.创建数据库的连接对象  jdbc:mysql://localhost:3306/tedu?
        Connection connection= DriverManager.getConnection(
              "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        //3.创建sql语句的执行对象
        Statement statement=connection.createStatement();
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入用户名：");
        String username=sc.nextLine();
        System.out.print("请输入密码：");
        String password=sc.nextLine();
        System.out.println("请输入昵称");
        String nickname=sc.nextLine();
        System.out.println("请输入年龄");
        int age=sc.nextInt();
        //4.执行sql语句  执行写操作（增删改）executeUpdate()    执行查操作executeQuery()
        //把外部变量值嵌入sql语句中   "+变量名+"
        statement.executeUpdate("INSERT INTO user(username, password,nickname,age) VALUES('"+username+"','"+password+"','"+nickname+"',"+age+")");
        System.out.println("数据插入成功");
        //断开数据库连接
        connection.close();

    }
}
